/***
 * Copyright (c) 2021-2031 murenchao
 * fig is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *       http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package cool.taomu.software.fig.mqtt.broker.impl.request

import cool.taomu.software.fig.mqtt.broker.MQTTCache
import cool.taomu.software.fig.mqtt.broker.impl.PublishObservable
import cool.taomu.software.fig.mqtt.broker.inter.IRequest
import cool.taomu.software.fig.mqtt.utils.CommonUtils
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.mqtt.MqttMessage
import org.slf4j.LoggerFactory

/**
 *  DISCONNECT 	|	14 	|	两个方向都允许	|	断开连接通知
 */
class MQTTDisconnect implements IRequest {
	val LOG = LoggerFactory.getLogger(MQTTDisconnect);
	
	MQTTCache cache = MQTTCache.instance;
	
	override request(ChannelHandlerContext ctx, MqttMessage mqttMessage) {
		LOG.info("执行了MQTT Disconnect 命令")
		var clientId = CommonUtils.getClientId(ctx.channel);
		 // 1. 清理会话 或 重新设置该客户端会话状态
		 var clientSession =  cache.loadSession(clientId);
		 if(clientSession.cleanStatus){
		    cache.clearSession(clientId);
		 }
		 PublishObservable.instance.unregister(clientId);
		 ctx.close;
		 return null;
	}
	
}
